/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package artaza.service;

import artaza.clasesdao.FincaDAO;
import artaza.persistencia.Finca;
import com.google.appengine.api.datastore.Key;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
/**
 *
 * @author AGM
 */
@Service
public class FincaServiceImpl implements FincaService{

    private  FincaDAO fincaDAO;

    @Autowired
    @Required
    public void setFincaDAO(final FincaDAO fincaDAO) {
        this.fincaDAO = fincaDAO;
    }

    
    @Override
    public void create(final Finca finca){
        fincaDAO.insert(finca);
}


    
    @Override
    public void remove (final Key id){
        Finca f = fincaDAO.findByPK(Finca.class,id);
        fincaDAO.remove(f);
    }

    
    @Override
    public Collection<Finca> getAllFincas() {
       Collection<Finca> fincas = fincaDAO.getAll(Finca.class);
       return fincas;
    }

    @Override
    public List<Finca> buscar(int criterio, String palabra) {

        List<Finca> espacios = new ArrayList();

        switch (criterio) {
            case 1: {
              Finca f = fincaDAO.findByNombref(Finca.class, palabra);
              break;
            }

            case 2: {
              List<Finca> results = fincaDAO.getAll(Finca.class);
              for(Finca finca:results){
                  if(finca.getOcupado().contains(palabra)==false){
                     espacios.add(finca);
                  }
              }
              break;
            }
        }
        return espacios;
    }

    @Override
    public Finca buscar2(String palabra) {
        Finca f = fincaDAO.findByNombref(Finca.class, palabra);
        return f;
    }
    @Override
    public Finca findByPK(Key id) {
        Finca f = fincaDAO.findByPK(Finca.class, id);
        return f;
    }


    @Override
    public void update(Finca f) {
        fincaDAO.update(f);
    }

}